package com.crocodileQ.aspect;

import com.crocodileQ.annotation.AutoStatistics;
import com.crocodileQ.enums.OperationType;
import com.crocodileQ.service.VisitsService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class AutoStatisticAspect {

    @Autowired
    private VisitsService visitsService;


    /**
     * 切入点,切点表达式 execution: 任意返回值 哪个包下的.所有类.所有方法(所有参数） && 注解 AutoFill
     */
    @Pointcut("execution(* com.crocodileQ.controller.*.*(..)) && @annotation(com.crocodileQ.annotation.AutoStatistics)")
    public void autoStatisticPointCut(){}


    @After("autoStatisticPointCut()")
    public void AutoStatistic(JoinPoint joinpoint){
        //获取到当前被拦截的方法上的数据库操作类型
        MethodSignature signature = (MethodSignature) joinpoint.getSignature(); //方法签名对象
        AutoStatistics autoFill = signature.getMethod().getAnnotation(AutoStatistics.class);//获取方法上的注解对象
        String operationType = autoFill.value();//数据库操作类型


        visitsService.updateVisits(operationType);

    }

}
